package top.wshape1.shiziapi.project.controller;

import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.wshape1.shiziapi.common.constant.AuthConstant;
import top.wshape1.shiziapi.common.domain.CommonUser;
import top.wshape1.shiziapi.common.domain.vo.PageVo;
import top.wshape1.shiziapi.common.utils.Base64Util;
import top.wshape1.shiziapi.common.utils.R;
import top.wshape1.shiziapi.project.common.annotation.EditHistoryLog;
import top.wshape1.shiziapi.project.common.constant.EditConstant;
import top.wshape1.shiziapi.project.domain.vo.ProjectVo;
import top.wshape1.shiziapi.project.service.EditHistoryService;
import top.wshape1.shiziapi.project.service.ProjectService;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-09-12
 */

@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @Resource
    private EditHistoryService editHistoryService;

    /**
     * 获取我的项目或我参与的项目
     *
     * @param type       my or his
     * @param page       当前页码
     * @param limit      数量限制
     * @param keyword    关键词查询
     * @param userBase64
     * @return
     */
    @GetMapping("/list/{type}")
    public R<PageVo> projectListPage(@PathVariable("type") String type,
                                     @RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                     @RequestParam(value = "limit", required = false, defaultValue = "12") Long limit,
                                     @RequestParam(value = "keyword", required = false) String keyword,
                                     @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64
    ) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        if ("my".equals(type))
            return R.ok(projectService.projectListPageMy(commonUser.getId(), page, limit, keyword));
        else if ("his".equals(type))
            return R.ok(projectService.projectListPageHis(commonUser.getId(), page, limit, keyword));
        return R.error();
    }

    /**
     * 创建项目
     *
     * @param userBase64
     * @param projectVo
     * @return
     */
    @PostMapping("/create")
    public R<Void> createProject(@RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64,
                                 @RequestBody @Validated ProjectVo projectVo) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        projectService.createProject(commonUser.getId(), projectVo);

        return R.ok();
    }

    /**
     * 修改项目信息
     *
     * @param file        项目封面
     * @param id
     * @param name
     * @param description
     * @param isPrivate
     * @param userBase64
     * @return
     */
    @PostMapping("/modify")
    @EditHistoryLog(type = EditConstant.EDIT_TYPE_MODIFY, projectIdParamName = "id")
    public R<Void> modifyProject(@RequestParam(value = "file", required = false) MultipartFile file,
                                 @RequestParam("id") Long id,
                                 @RequestParam("name") String name,
                                 @RequestParam("description") String description,
                                 @RequestParam("isPrivate") Integer isPrivate,
                                 @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        ProjectVo projectVo = new ProjectVo();
        projectVo.setId(id);
        projectVo.setName(name);
        projectVo.setDescription(description);
        projectVo.setIsPrivate(isPrivate);

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

//        System.out.println(projectVo);

        projectService.modifyProject(commonUser.getId(), projectVo, file);

        return R.ok();
    }

    /**
     * 获取项目成员列表
     *
     * @param projectId
     * @param page
     * @param limit
     * @param userBase64
     * @return
     */
    @GetMapping("/member")
    public R<PageVo> getProjectMemberPage(@RequestParam("id") String projectId,
                                          @RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                          @RequestParam(value = "limit", required = false, defaultValue = "6") Long limit,
                                          @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        PageVo pageVo = projectService.getProjectMemberPage(commonUser, projectId, page, limit);

        return R.ok(pageVo);
    }

    /**
     * 删除项目
     *
     * @param projectId
     * @param userBase64
     * @return
     */
    @GetMapping("/delete")
    public R<Void> deleteProject(@RequestParam("id") Long projectId,
                                 @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        projectService.deleteProject(commonUser.getId(), projectId);

        return R.ok();
    }

    /**
     * 发送消息 邀请成员加入项目
     *
     * @param projectId
     * @param memberId
     * @param userBase64
     * @return
     */
    @GetMapping("/member/invite")
    public R<Void> inviteToProject(@RequestParam("projectId") Long projectId,
                                   @RequestParam("memberId") Long memberId,
                                   @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        projectService.inviteMember(commonUser.getId(), commonUser.getNickname(), projectId, memberId);

        return R.ok();
    }

    /**
     * 从项目中踢出某个成员
     *
     * @param projectId
     * @param memberId
     * @param userBase64
     * @return
     */
    @GetMapping("/member/kick")
    public R<Void> kickFromProject(@RequestParam("projectId") Long projectId,
                                   @RequestParam("memberId") Long memberId,
                                   @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        projectService.kickMember(commonUser.getId(), projectId, memberId);

        return R.ok();
    }

    @GetMapping("/member/accept")
    public R<Void> acceptMemberInvite(@RequestParam("msgId") Long msgId,
                                      @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        projectService.acceptInvite(msgId, commonUser.getId(), userBase64);

        return R.ok();
    }

    @GetMapping("/member/reject")
    public R<Void> rejectMemberInvite(@RequestParam("msgId") Long msgId,
                                      @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        projectService.rejectInvite(msgId, commonUser, userBase64);

        return R.ok();
    }

    /**
     * 获取项目的名字
     *
     * @param projectId
     * @return
     */
    @GetMapping("/name")
    public R<String> getProjectName(@RequestParam("id") Long projectId) {

        String projectName = projectService.getNameById(projectId);

        return R.ok().putData(projectName);
    }

    @PostMapping("/history/list")
    public R<PageVo> getHistoryList(@RequestParam("id") Long projectId,
                                    @RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                    @RequestParam(value = "limit", required = false, defaultValue = "6") Long limit) {
        PageVo vo = editHistoryService.getHistoryList(projectId, page, limit);
        return R.ok(vo);
    }

}
